package com.example.demo.service;

import com.example.demo.dao.UserDao;
import com.example.demo.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author Keyon
 * @Date 2019/11/9 16:33
 * @Desc
 */
@Slf4j
@Service
public class UserService {
    @Autowired
    UserDao userMapper;

    public List<User> getAllUser() {
        return userMapper.getAllUser();
    }

    public User getUserById(long id) {
        return userMapper.getUserById(id);
    }

    public void updateUserById(long id, String userName) {
        User user = getUserById(id);
        if (user == null) {
            return;
        }
        user.setUserName(userName);
        userMapper.updateUserById(user);
    }

    public String login(String username, String password) {
        User user = userMapper.getUserByUserName(username);
        log.info("尝试登录");

        if (user == null) {
            log.info("用户不存在");
            return "error";
        }
        if (user.getPassword().equals(password)) {
            log.info("登录成功");
            log.info(user.toString());
            return "success";
        } else {
            log.info("登录失败");
            log.info(user.toString());
            return "failed";
        }
    }

}
